package com.shushan.discount;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.Constants;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.UmengEventUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @ClassName: CouponExchangeActivity
 * @Desciption: 优惠券兑换
 * @author: yichaohua
 * @date: 2018-08-29
 */
public class CouponExchangeActivity extends NavigationLoseActivity implements View.OnClickListener, TextWatcher {

    private static final String TAG = "CouponExchangeActivity";


    private EditText exchangeEdit;
    private Button activityButton;
    private ImageView clearImg;
    private Dialog activitySuccessDialog;
    private Dialog activityFaildDialog;
    private BabyApplication babyApplication = BabyApplication.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.coupon_exchange_activity);

        setTitle("优惠券兑换");

        initViwe();

        UmengEventUtil.onEvent(babyApplication,"coupon_exchange_page_id");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(activitySuccessDialog != null){
            activitySuccessDialog.dismiss();
        }

        if(activityFaildDialog != null){
            activityFaildDialog.dismiss();
        }
    }

    private void initViwe() {
        exchangeEdit = findViewById(R.id.coupon_exchange_code_edit);
        exchangeEdit.addTextChangedListener(this);
        activityButton = findViewById(R.id.coupon_exchange_btn);
        activityButton.setOnClickListener(this);
        clearImg = findViewById(R.id.coupon_exchange_clear_img);
        clearImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.coupon_exchange_btn:
                activityCoupon();
                UmengEventUtil.onEvent(babyApplication,"coupon_exchange_activity_click_id");
                break;
            case R.id.coupon_exchange_clear_img:
                exchangeEdit.setText("");
                break;
            default:
                break;
        }
    }

    private void activityCoupon() {
        String coupon = exchangeEdit.getText().toString();

        showWaitDialog();

        int userId = BabyApplication.getInstance().getCurrentUseId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.ACTIVITY_COUPON);
        urlParse.putValue("user_id", userId)
                .putValue("coupon", coupon);

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            showSuccessDialog(jsonObject.optInt("vip_time",30));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showFaildDialog(netRetBean.getServerMsg());
                    }
                });
    }

    private void showFaildDialog(String msg) {
        activityFaildDialog = new Dialog(this,R.style.Dialog_Translucent_Background);
        activityFaildDialog.setCanceledOnTouchOutside(true);
        activityFaildDialog.setContentView(R.layout.activity_faild_dialog);
        TextView msgText = activityFaildDialog.findViewById(R.id.activity_faild_msg_tv);
        msgText.setText(msg);

        if(!activityFaildDialog.isShowing()){
            activityFaildDialog.show();
        }
    }

    private void showSuccessDialog(int day) {
        activitySuccessDialog = new Dialog(this,R.style.Dialog_Translucent_Background);
        activitySuccessDialog.setContentView(R.layout.activity_success_dialog);

        TextView dayText = activitySuccessDialog.findViewById(R.id.activity_sucess_day_tv);
        dayText.setText("恭喜您已获得" + day + "天VIP使用时间");
        Button button = activitySuccessDialog.findViewById(R.id.activity_sucess_btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackAction(100);
            }
        });

        activitySuccessDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
                    onBackAction(100);
                }

                return false;
            }
        });

        if(!activitySuccessDialog.isShowing()){
            activitySuccessDialog.show();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if(s.length() >= 10){
            activityButton.setEnabled(true);
        }else {
            activityButton.setEnabled(false);
        }

        if(s.length() == 0){
            clearImg.setVisibility(View.GONE);
        }else {
            clearImg.setVisibility(View.VISIBLE);
        }
    }
}
